package nsu.edu.zsq.controller.sys;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;

import nsu.edu.zsq.bean.SchMajor;
import nsu.edu.zsq.bean.Student;
import nsu.edu.zsq.common.JsonData;
import nsu.edu.zsq.exception.ParamException;
import nsu.edu.zsq.param.PageQuery;
import nsu.edu.zsq.service.sys.CourseService;
import nsu.edu.zsq.service.sys.SchMajorService;
import nsu.edu.zsq.service.sys.StudentService;
import nsu.edu.zsq.util.UploadFileUtil;

/** 
* Description: 学生的controller类<br>
*/
@RequestMapping("/sys/student/info")
@Controller
public class StudentController {
    
    @Autowired
    private StudentService studentService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private SchMajorService schMajorService;
    
    /**
     * 获取院系+专业的tree结构数据
     * @return JsonData
     */
    @RequestMapping("/schOrgWithMajorTree")
    @ResponseBody
    public JsonData schOrgWithMajorTree() {
        String listSchOrgWithMajorTree = courseService.listSchOrgWithMajorTree();
        List<SchMajor> listSchMajor = schMajorService.listSchMajor();
        Map<String, Object> map = new HashMap<String, Object>(2);
        map.put("listSchOrgWithMajorTree", listSchOrgWithMajorTree);
        map.put("listSchMajor", listSchMajor);
        return JsonData.success(map);
    }
    
    /**
     * 根据专业代码获取学生的分页数据
     * @param majorNo 专业代码
     * @param pageQuery 分页数据
     * @return JsonData
     */
    @RequestMapping("/listStudent")
    @ResponseBody
    public JsonData listStudentWithPage(@RequestParam("majorNo") String majorNo, PageQuery pageQuery) {
        PageInfo<Student> listStudent = studentService.listSchMajorWithPage(majorNo, pageQuery);
        return JsonData.success(listStudent);
    }
    /**
     * 单个或者批量删除学生
     * @param stuId 学生id数组
     * @return JsonData
     */
    @RequestMapping("/delStudent")
    @ResponseBody
    public JsonData delStudent(@RequestParam("stuId[]")int stuId[]) {
        if (stuId.length > 1) {
            // 批量删除
            studentService.deleteBatchStudent(stuId);
        } else if (stuId.length == 1) {
            // 单个删除
            studentService.deleteStudent(stuId[0]);
        } else {
            throw new ParamException("stuId无参数");
        }
        return JsonData.success();
    }
    
    /**
     * 跳转到添加学生信息页面
     * @param majorNo 专业代码
     * @param schoolDep 院系名称
     * @param model 
     * @return String manager/student/student_add.html
     */
    @RequestMapping("/toAddStudent")
    public String toAddStudent(@RequestParam("majorNo")String majorNo, 
            @RequestParam("schoolDep")String schoolDep, Model model) {
        model.addAttribute("majorNo", majorNo);
        model.addAttribute("schoolDep", schoolDep);
        return "manager/student/student_add.html";
    }
    
    /**
     * 跳转到编辑学生信息页面
     * @param stuId 学生id
     * @param model 
     * @return String manager/student/student_edit.html
     */
    @RequestMapping("/toEditStudent")
    public String toEditStudent(@RequestParam("stuId")int stuId, Model model) {
        Student student = studentService.getStudent(stuId);
        model.addAttribute("student", student);
        return "manager/student/student_edit.html";
    }
    
    /**
     * 保存一条学生信息
     * @param student 学生信息
     * @return JsonData
     */
    @RequestMapping("/saveStudent")
    @ResponseBody
    public JsonData saveStudent(Student student) {
        studentService.saveStudent(student);
        return JsonData.success();
    }
    
    /**
     * 修改一条学生信息
     * @param student 学生信息
     * @return JsonData
     */
    @RequestMapping("/editStudent")
    @ResponseBody
    public JsonData editStudent(Student student) {
        studentService.updateStudent(student);
        return JsonData.success();
    }
    
    /**
     * 重置学生登录密码
     * @param stuId 学生id
     * @return JsonData
     */
    @RequestMapping("/resetPassword")
    @ResponseBody
    public JsonData resetPassword(@RequestParam("stuId")int stuId) {
        studentService.resetPassword(stuId);
        return JsonData.success();
    }
    
    

    
    /**
     * 从excel中导入学生数据
     * @param excelFile
     * @return JsonData
     */
    @RequestMapping("/importStudent")
    @ResponseBody
    public JsonData importStudent(@RequestParam("excelFile")MultipartFile excelFile) {
        // 1.解析excel文件，保存到数据库
        studentService.importStudentFromExcel(excelFile);
        // 2.启动多线程保存文件（先保存文件会破坏MultipartFile数据）
        new Thread() {
            public void run() {
                UploadFileUtil.uploadFile(excelFile);
            }
        }.start();

        return JsonData.success();
    }
    
    /**
     * 导出学生数据到excel文件
     * @param response 请求
     */
    @RequestMapping("/exportStudent")
    public void exportStudent(HttpServletResponse response) {
        studentService.exportStudentToExcel(response);
    }
    
}
